// /*
//  * @Author: WanYiMing
//  * @Date:2024-05-31-11:13
//  * @Description:
//  */

using System;
using Code.Core.CoreBaseClass;
using Code.GameLogic.Common.Event;
using Code.GameLogic.Common.GException;
using Code.GameLogic.Map.Vo;
using Code.GameLogic.XianJian.Vo;
using QFramework;
using UnityEngine;
using UnityEngine.EventSystems;
using Input = UnityEngine.Input;

namespace Code.GameLogic.XianJian.UI
{
    public class XianJianUIController:BaseController,IPointerClickHandler
    {
        private bool _onSelect;
        
        private XianJianUIBo _xianJianUI;
        
        public override IArchitecture GetArchitecture()
        {
            return MainApp.Interface;
        }

        public XianJianUIBo XianJianUI
        {
            get => _xianJianUI;
            set => _xianJianUI = value;
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            PrintLogger("图标被点击！！"+_xianJianUI.BaseDescription.TypeName);
            TigerPlaceShow();
        }


        private void Update()
        {
            //右键撤销选择
            if(Input.GetMouseButtonDown(1)&&_onSelect)
            {
                TigerPlaceShow();
            }
            
            //左键点击确认
            if(Input.GetMouseButtonDown(0)&&_onSelect)
            {
                GameObject selectBlock = GetSelectBlock();
                if (selectBlock is not null)
                {
                    if (CheckBlockIsBlack(selectBlock))
                    {
                        TigerBuildingXianJian(selectBlock);
                        TigerPlaceShow();
                    }
                }
            }
        }

        private bool CheckBlockIsBlack(GameObject blockGameObject)
        {
            

            if (blockGameObject.name.IndexOf("|", StringComparison.Ordinal)<0)
            {
                return false;
            }
            
            CheckBlockVo checkBlockVo = new CheckBlockVo();
            checkBlockVo.BlockName = blockGameObject.name;
            checkBlockVo.Type = 0;
            try
            {
                this.SendEvent(checkBlockVo);
            }
            catch (CheckMessageException e)
            {
                PrintLogger(e.Message);
                return false;
            }

            return true;
        }


        private void TigerBuildingXianJian(GameObject selectBlock)
        {
            BuilderXianJianVo builderXianJianVo = new BuilderXianJianVo();
            builderXianJianVo.XianJianTypeCode = _xianJianUI.BaseDescription.TypeCode;
            builderXianJianVo.BlockGo = selectBlock;
            this.SendEvent(builderXianJianVo);
        }


        private void TigerPlaceShow()
        {
            //放置视图显示
            PlaceBlockShowEs placeBlockShowEs = new PlaceBlockShowEs();
            //鼠标样式显示
            MouseShowEs mouseShowEs = new MouseShowEs();
            if (_onSelect)
            {
                placeBlockShowEs.ShowType = 0;
                _onSelect = false;
                mouseShowEs.IsShow = false;
            }
            else
            {
                placeBlockShowEs.ShowType = 1;
                _onSelect = true;
                mouseShowEs.IsShow = true;
                mouseShowEs.Space=_xianJianUI.ActArea;
            }
            this.SendEvent(mouseShowEs);
            this.SendEvent(placeBlockShowEs);
        }
        

        private GameObject GetSelectBlock()
        {
            Vector3 mousePos = Input.mousePosition;
            mousePos.z = 10;
            Vector3 screenPos = Camera.main.ScreenToWorldPoint(mousePos);
            RaycastHit2D hit = Physics2D.Raycast(screenPos, Vector2.zero);
            if (hit)
            {
                return hit.collider.gameObject;
            }
            return null;
        }



    }
}